//
//  MHSettingViewController.m
//  MHO
//
//  Created by papo on 2/24/14.
//  Copyright (c) 2014 maihoang. All rights reserved.
//

#import "MHSettingViewController.h"
#import "MHNavibarView.h"
#import "Lib.h"
#import "DownloadManager.h"
#import "MHDownloadViewController.h"
#import "SettingManager.h"

@interface MHSettingViewController ()
{
    NSArray* titleArray;
    MHNavibarView* navibarView;
}
@end

@implementation MHSettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    titleArray = @[@"Đang tải", @"Chỉ tải trong môi trường wifi", @"Tải xong thì tự xoá", @"Tải xong tự động cài đặt", @"Không khoá màn hình khi đang tải", @"Tiết kiệm lưu lượng", @"Nhạc báo hiệu", @"Hiển thị số lượng app cần nâng cấp"];
    
    navibarView = [MHNavibarView createWithTitle:@"Thiết lập cơ bản" leftButton:nil rightButton:nil searchButton:NO];
    [navibarView setActionSelectorDelegate:self left:@selector(leftButtonPressed:) right:nil search:nil];
    [navibarView setRootViewController:self];
    CGRect frame = navibarView.frame;
//    frame.origin.y  += 20;
    navibarView.frame = frame;
    [self.view addSubview:navibarView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    if (section == 0) {
        return 1;
    }
    return titleArray.count - 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        if (indexPath.section == 0) {
            UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(250, 0, 50, 44)];
            label.text = @"0";
            label.tag = 123;
            label.textAlignment = NSTextAlignmentRight;
            label.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview:label];
        }
        else {
            float offset = 0;
            if ([Lib isIos7]) {
                offset = 40;
            }
            UISwitch* switc = [[UISwitch alloc] initWithFrame:CGRectMake(220 + offset, 5, 40, 34)];
            switc.tag = indexPath.row + 123;
            [cell.contentView addSubview:switc];
        }
    }
    
    
    // Configure the cell...
    if (indexPath.section == 0) {
        cell.textLabel.text = [titleArray objectAtIndex:0];
        UILabel* label = (UILabel*)[cell.contentView viewWithTag:123];
        label.text = [NSString stringWithFormat:@"%lu", (unsigned long)[[DownloadManager sharedManager] listDownload].count];
    }
    else {
        cell.textLabel.text = titleArray[indexPath.row + 1];
        UISwitch* swit = (UISwitch*)[cell.contentView viewWithTag:123 + indexPath.row];
        [swit addTarget:self action:@selector(switchChangeValue:) forControlEvents:UIControlEventValueChanged];
        [swit setOn:[[SettingManager shared] settingAtIndex:indexPath.row]];
    }
    [cell.textLabel setFont:[UIFont systemFontOfSize:13]];
    cell.textLabel.numberOfLines = 2;
    CGRect frame = cell.textLabel.frame;
    frame.size.width = 200;
    [cell.textLabel setFrame:frame];
    [cell.contentView sendSubviewToBack:cell.textLabel];
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];

    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
 
 */

- (void)leftButtonPressed: (id)sender {
//    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)switchChangeValue:(UISwitch*)swit {
    NSInteger index = swit.tag - 123;
    [[SettingManager shared] setSettingAtIndex:index value:swit.on];
}

@end
